package com.twelve.all.base;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.twelve.all.nfc.BasicActivity;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * author: 张龙臣
 * date: 2018-08-29.
 */

public abstract class PermissionActivity extends BasicActivity implements EasyPermissions.PermissionCallbacks {

    public static final int CONTACTS_CODE = 271;
    public static final int LOCATION_CODE = 272;
    public static final int STORAGE_CODE = 273;
    public static final int CALENDAR_CODE = 274;
    public static final int PHONE_CODE = 275;
    public static final int SMS_CODE = 276;
    public static final int CAMERA_CODE = 277;
    public static final int MICROPHONE_CODE = 278;
    public static final int SENSORS_CODE = 279;
    public static final int INSTALL_PACKAGE_CODE = 280;
    protected int mWhich = 0;
    /**
     * 手机联系人
     */
    public static final String[] CONTACTS = new String[]{
            Manifest.permission.WRITE_CONTACTS,
            Manifest.permission.GET_ACCOUNTS,
            Manifest.permission.READ_CONTACTS
    };
    /**
     * 定位
     */
    public static final String[] LOCATION = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    };
    /**
     * 读写储存
     */
    public static final String[] STORAGE = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    /**
     * 读写日历
     */
    public static final String[] CALENDAR = new String[]{
            Manifest.permission.READ_CALENDAR,
            Manifest.permission.WRITE_CALENDAR
    };
    /**
     * 打电话相关
     */
    public static final String[] PHONE = new String[]{
            Manifest.permission.READ_CALL_LOG,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.WRITE_CALL_LOG,
            Manifest.permission.USE_SIP,
            Manifest.permission.PROCESS_OUTGOING_CALLS,
            Manifest.permission.ADD_VOICEMAIL
    };
    /**
     * 短信相关
     */
    public static final String[] SMS = new String[]{
            Manifest.permission.SEND_SMS,
            Manifest.permission.RECEIVE_SMS,
            Manifest.permission.READ_SMS,
            Manifest.permission.RECEIVE_WAP_PUSH,
            Manifest.permission.RECEIVE_MMS
    };
    /**
     * 相机
     */
    public static final String[] CAMERA = new String[]{Manifest.permission.CAMERA};
    /**
     * 麦克风
     */
    public static final String[] MICROPHONE = new String[]{Manifest.permission.RECORD_AUDIO};
    /**
     * 传感器  api 20 才有的权限
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
    public static final String SENSORS = Manifest.permission.BODY_SENSORS;

    //待测
    public static final String[] INSTALL_PACKAGE = new String[]{Manifest.permission.INSTALL_PACKAGES};

    /**
     * 已有权限，对应code直接操作
     */
    protected abstract void onGranted(int requestCode);


    protected abstract void onProhibited(int requestCode);


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @AfterPermissionGranted(CAMERA_CODE)
    public void cameraTask() {
        Log.e("Permission", "------------cameraTask----------- ");
        if (EasyPermissions.hasPermissions(this, CAMERA)) {
            onGranted(CAMERA_CODE);
        } else {
//            EasyPermissions.requestPermissions(this, "需要获取相机权限", CAMERA_CODE, CAMERA);
            ActivityCompat.requestPermissions(this, CAMERA, CAMERA_CODE);
        }
    }

    @AfterPermissionGranted(STORAGE_CODE)
    public void strorageTask() {
        Log.e("Permission", "------------strorageTask----------- ");
        if (EasyPermissions.hasPermissions(this, STORAGE)) {
            onGranted(STORAGE_CODE);
        } else {
            //弹框提示再请求
//            EasyPermissions.requestPermissions(this, "需要获取存储权限", STORAGE_CODE, STORAGE);
            //直接请求
            ActivityCompat.requestPermissions(this, STORAGE, STORAGE_CODE);
        }
    }

    @AfterPermissionGranted(CALENDAR_CODE)
    public void calendarTask() {
        if (EasyPermissions.hasPermissions(this, CALENDAR)) {
            onGranted(CALENDAR_CODE);
        } else {
//            EasyPermissions.requestPermissions(this, "需要获取存储权限", CALENDAR_CODE, CALENDAR);
            ActivityCompat.requestPermissions(this, CALENDAR, CALENDAR_CODE);
        }
    }

    @AfterPermissionGranted(MICROPHONE_CODE)
    public void microphoneTask() {
        if (EasyPermissions.hasPermissions(this, MICROPHONE)) {
            onGranted(CALENDAR_CODE);
        } else {
//            EasyPermissions.requestPermissions(this, "需要获取存储权限", CALENDAR_CODE, CALENDAR);
            ActivityCompat.requestPermissions(this, MICROPHONE, CALENDAR_CODE);
        }
    }

    @AfterPermissionGranted(LOCATION_CODE)
    public void locationTask() {
        if (EasyPermissions.hasPermissions(this, LOCATION)) {
            onGranted(LOCATION_CODE);
        } else {
//            EasyPermissions.requestPermissions(this, "需要获取存储权限", LOCATION_CODE, LOCATION);
            ActivityCompat.requestPermissions(this,LOCATION,LOCATION_CODE);
        }
    }

    @AfterPermissionGranted(PHONE_CODE)
    public void callPhoneTask() {
        if (EasyPermissions.hasPermissions(this, PHONE)) {
            onGranted(PHONE_CODE);
        } else {
//            EasyPermissions.requestPermissions(this, "需要获取存储权限", LOCATION_CODE, LOCATION);
            ActivityCompat.requestPermissions(this,PHONE,PHONE_CODE);
        }
    }

    @AfterPermissionGranted(INSTALL_PACKAGE_CODE)
    public void installPackageTask() {
        if (EasyPermissions.hasPermissions(this, INSTALL_PACKAGE)) {
            onGranted(INSTALL_PACKAGE_CODE);
        } else {
//            EasyPermissions.requestPermissions(this, "需要获取存储权限", INSTALL_PACKAGE_CODE, INSTALL_PACKAGE);
            ActivityCompat.requestPermissions(this, INSTALL_PACKAGE, INSTALL_PACKAGE_CODE);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        Log.e("Permission", "------------onPermissionsGranted----------- ");
//        onGranted(requestCode);//重复的操作
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        Log.e("Permission", "------------onPermissionsDenied----------- ");
        onProhibited(requestCode);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}
